package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.IUserDao;
import com.dao.UserDaoImp;
import com.vo.Users;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String name = request.getParameter("userName");
		String pwd = request.getParameter("passWord");
		List<Users> list = new ArrayList<Users>();
		Users user = new Users();
		Users users = new Users();
		users.setEu_user_name(name);
		users.setEu_password(pwd);
		
		IUserDao dao = new UserDaoImp();
		HttpSession session = request.getSession();
		boolean flag = false;
		try {
			flag = dao.login(users);
			session.setAttribute("username",name);
		} catch (Exception e) {

			e.printStackTrace();
		}
		if (flag) {
			//设置权限
			session.setAttribute("Uname", name);
			try {
				list = dao.getAllData();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for(int a=0;a<list.size();a++){
				System.out.println(name);
				if(name.equals(list.get(a).getEu_user_name())){
					user = list.get(a);
					
					if(user.getEu_status()== 1){
						response.sendRedirect("indexProductServlet");
					}else{
						request.getRequestDispatcher("manage/index.jsp").forward(request,
								response);
					}
				}
			}
			
			
			

		} else {
			out.print("<HTML>" + "<BR>");
			out.print("<HEAD><TITLE>登录信息</TITLE></HEAD>" + "<BR>");
			out.print("<BODY>" + "<BR>");
			out.print("<TABLE BORDER=1>" + "<BR>");
			out.print("<TR>" + "<BR>");
			out.print("<TD>" + "<BR>");
			out.print("USERNAME PR PASSWORD ERROR");
			out.print("</TD>" + "<BR>");
			out.print("<TD>" + "<BR>");
			out.print("登录失败！");
			out.print("</TD>" + "<BR>");
			out.print("</TR>" + "<BR>");
			out.print("</TABLE>" + "<BR>");
			out.print("</BODY>" + "<BR>");
			out.print("</HTML>" + "<BR>");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
